﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using Business;
using Entities;

namespace ServicesDirectory.UserControl
{
    public partial class ListGovernment : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["btnFilter"] = "All";
                LoadAll();
            }


            All.Click += All_Click;
            lbt09.Click += lbt09_Click;
            lbtA.Click += lbtA_Click;
            lbtF.Click += lbtF_Click;
            lbtK.Click += lbtK_Click;
            lbtO.Click += lbtO_Click;
            lbtS.Click += lbtS_Click;
            lbtW.Click += lbtW_Click;
            chkInactive.CheckedChanged += chkInactive_CheckedChanged;
            lbtGovName.Click += lbtGovName_Click;
        }

        /// <summary>
        /// Handles the ItemDataBound event of the Repeater1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblIsactive = e.Item.FindControl("lblIsactive") as Label;
            if (lblIsactive != null)
                lblIsactive.Text = lblIsactive.Text == "True" ? "Yes" : "No";
        }



        /// <summary>
        /// Handles the Click event of the lbtGovName control - sort list ascending or descending
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtGovName_Click(object sender, EventArgs e)
        {

            if (ViewState["sort"] == null || ViewState["sort"].ToString().Equals("asc"))
            {
                var result = new Collection<GovOfficeRegion>(GovOfficeRegionBusiness.Instance.Filter(ViewState["filter"].ToString(), chkInactive.Checked).OrderBy(item => item.GovOfficeRegionName).ToList());
                Paging(result);
                ViewState["sort"] = "desc";
            }
            else
            {
                var result = new Collection<GovOfficeRegion>(GovOfficeRegionBusiness.Instance.Filter(ViewState["filter"].ToString(), chkInactive.Checked).OrderByDescending(item => item.GovOfficeRegionName).ToList());
                Paging(result);
                ViewState["sort"] = "asc";
            }
        }


        /// <summary>
        /// Handles the CheckedChanged event of the chkInactive control - Include in-active check box
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            if (ViewState["filter"] == null)
            {
                var result = GovOfficeRegionBusiness.Instance.GetGovOfficeRegion(chkInactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }
            else
            {
                var result = GovOfficeRegionBusiness.Instance.Filter(ViewState["filter"].ToString(), chkInactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }


        }

        #region filter funtion - do filfer base on keyword


        /// <summary>
        /// Handles the Click event of the lbtW control- filter by W X Y Z keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtW_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["filter"] = "wz";
            ViewState["btnFilter"] = "lbtW";
            var result = GovOfficeRegionBusiness.Instance.Filter("wz", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtS control - filter by S T U V keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtS_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["filter"] = "sv";
            ViewState["btnFilter"] = "lbtS";
            var result = GovOfficeRegionBusiness.Instance.Filter("sv", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtO control - filter by O P Q R keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtO_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "lbtO";
            ViewState["filter"] = "or";
            var result = GovOfficeRegionBusiness.Instance.Filter("or", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtK control - filter by K L M N keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtK_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "lbtK";
            ViewState["filter"] = "kn";
            var result = GovOfficeRegionBusiness.Instance.Filter("kn", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtF control - filter by F G H I J keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtF_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "lbtF";
            ViewState["filter"] = "fj";
            var result = GovOfficeRegionBusiness.Instance.Filter("fj", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtA control filter by  A B C D E keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtA_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "lbtA";
            ViewState["filter"] = "ae";
            var result = GovOfficeRegionBusiness.Instance.Filter("ae", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbt09 control filter by 0-9 keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbt09_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "lbt09";
            ViewState["filter"] = "09";
            var result = GovOfficeRegionBusiness.Instance.Filter("09", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }


        /// <summary>
        /// Handles the Click event of the All control- filter ALL
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void All_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            ViewState["btnFilter"] = "All";
            ViewState["filter"] = "all";
            var result = GovOfficeRegionBusiness.Instance.Filter("all", chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        #endregion

        /// <summary>
        /// Loads all.
        /// </summary>
        private void LoadAll()
        {
            txtPageNumber.Text = "1";
            ViewState["filter"] = "all";
            var result = GovOfficeRegionBusiness.Instance.GetGovOfficeRegion(chkInactive.Checked);

            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Pagings the specified list.
        /// </summary>
        /// <param name="listGov">The list gov.</param>
        private void Paging(Collection<GovOfficeRegion> listGov)
        {
            if (ViewState["btnFilter"] != null)
            {
                ((LinkButton)FindControl(ViewState["btnFilter"].ToString())).ForeColor = Color.FromName("#99ff00");
            }
            lblTotalPage.Text = (listGov.Count % 15 == 0 ? listGov.Count / 15 : listGov.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
            //paging on list result
            var pagedList = GovOfficeRegionBusiness.Instance.Paging(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, listGov);
            //bind paged data
            if (pagedList != null && pagedList.Count > 0)
            {
                pnNoRecord.Visible = false;
                Repeater1.DataSource = pagedList;
                Repeater1.DataBind();
            }
            else
            {
                txtPageNumber.Text = "0";
                lblTotalPage.Text = "0";
                pnNoRecord.Visible = true;
                Repeater1.DataSource = null;
                Repeater1.DataBind();
            }
        }

        #region paging buttons

        protected void CmdFisrt_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            var result = GovOfficeRegionBusiness.Instance.Filter(ViewState["filter"].ToString(), chkInactive.Checked);

            if (result != null)
            {
                Paging(result);
            }
        }

        protected void CmdBack_Click1(object sender, EventArgs e)
        {
            if (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) > 1)
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) - 1).ToString(CultureInfo.InvariantCulture);
                var result =
                    GovOfficeRegionBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), chkInactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }
        }

        protected void CmdNext_Click1(object sender, EventArgs e)
        {
            if (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) < int.Parse(lblTotalPage.Text, CultureInfo.CurrentCulture))
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) + 1).ToString(CultureInfo.InvariantCulture);
                var result =
                    GovOfficeRegionBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), chkInactive.Checked);
                if (result != null)
                {

                    Paging(result);
                }
            }
        }

        protected void CmdLast_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            var result =
                        GovOfficeRegionBusiness.Instance.Filter(
                            ViewState["filter"].ToString(), chkInactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        #endregion

        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="CommandEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
        {
            if (e.CommandName == "GovClick")
            {
                Response.Redirect("GovOfficeRegionDetail.aspx?GovId=" + e.CommandArgument.ToString());

            }
        }

        /// <summary>
        /// Sorts the list.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public Collection<GovOfficeRegion> SortList(Collection<GovOfficeRegion> list)
        {
            if (ViewState["sort"] == null || ViewState["sort"].ToString().Equals("asc"))
            {
                var result = new Collection<GovOfficeRegion>(list.OrderBy(item => item.GovOfficeRegionName).ToList());
                ViewState["sort"] = "desc";
                return result;

            }
            else
            {
                var result = new Collection<GovOfficeRegion>(list.OrderByDescending(item => item.GovOfficeRegionName).ToList());
                ViewState["sort"] = "asc";
                return result;
            }
        }


    }




}